<template>
  <div id="search">
    <div class="header">
      <input type="text" name="" id="" placeholder="请输入内容" v-model="val" @keydown.enter="sear">
      <span @click="$router.go(-1)">取消</span>
    </div>
    <div class="lishi" v-show="val==''">
      <p><span>历史搜索</span>
        <van-icon name="delete-o" @click="dels" />
      </p>
      <ul class="jilu">
        <li v-for="(item,index) in $store.state.searchList" :key="index" v-show="index<cid">
          <span @click="tiao(item)">{{item}}</span>
          <van-icon name="close" @click="del(index)" />
        </li>
        <div v-if="$store.state.searchList.length>3">
          <p class="shou" @click="smail" v-show="cid!=3">收起</p>
          <p class="shou" @click="big" v-show="cid==3">全部</p>
        </div>
      </ul>
    </div>
    <div v-if="val!=''">
      <div v-if="flag">
        <keList :pagelists='list'></keList>
      </div>
      <div v-else>
        <van-empty class="custom-image" image="http://120.53.31.103:86/img/empty.0d284c2e.png" description="暂无课程" />
      </div>
    </div>
    <van-dialog v-model="show" title="提示" show-cancel-button @cancel="show=false" @confirm="yes">
      <p class="del">确认删除历史记录吗？</p>
    </van-dialog>
  </div>
</template>
<script>
import keList from '@/components/kelist'
import { search } from '@/api/user.js'
import { setInterval } from 'timers'
export default {
  components: { keList },
  data() {
    return {
      val: '',
      list: [],
      flag: false,
      cid: 3,
      show: false
    }
  },
  methods: {
    async sear() {
      this.$store.commit('search', this.val)
      let { data: data } = await search({
        limit: 10,
        page: 1,
        course_type: 0,
        keywords: this.val
      })
      console.log(data.data)
      this.list = data.data
      if (data.data.list.length >= 1) {
        this.flag = true
      } else {
        this.flag = false
      }
    },
    del(i) {
      this.$store.commit('del', i)
    },
    tiao(item) {
      this.val = item
      this.sear()
    },
    dels() {
      this.show = true
    },
    smail() {
      this.cid = 3
    },
    big() {
      this.cid = this.$store.state.searchList.length
    },
    yes() {
      this.$store.commit("dels")
    }
  },
  watch: {
    val(val) {
      if (val.length < 1) {
        this.list = []
      }
    }
  }
}
</script>
<style lang="scss" scoped>
#search {
  background: white;
  height: 100%;
}
.header {
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #eaeaea;
  input {
    width: 85%;
    border: none;
    background: #e4e7ed;
    height: 35px;
    border-radius: 25px;
    padding-left: 15px;
    font-size: 16px;
  }
  input::-webkit-input-placeholder {
    color: rgb(111, 111, 111);
  }
  span {
    color: #d2d2d1;
    font-size: 14px;
  }
}
.lishi {
  width: 100%;
  padding: 20px 15px;
  box-sizing: border-box;
  p {
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      font-size: 16px;
      font-weight: 900;
    }
    i {
      font-size: 16px;
      color: #ccc;
    }
  }
}
.jilu {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  overflow: hidden;
  li {
    margin: 5px 10px;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #e74d3d;
    background-color: rgba(231, 77, 61, 0.06);
    padding: 2px 5px;
    border: 1px solid #e74d3d;
    border-radius: 15px;
    i {
      margin-left: 5px;
    }
  }
  p {
    font-size: 14px;
  }
}
.shou {
  padding-left: 15px;
  font-size: 14px;
}
.del {
  text-align: center;
  font-size: 16px;
  margin: 30px 0;
}
/deep/.van-hairline--left {
  background: #e60012;
  color: white;
}
</style>


